<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>四则运算计算器</title>
  <!-- 引入 Vue.js -->
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  <style>
    body {
      font-family: Avenir, Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
      margin-top: 60px;
    }
  </style>
</head>

<body>
  <div id="app">
    <h1>四则运算计算器</h1>
    <input v-model="num1" type="number" placeholder="输入第一个数字">
    <select v-model="operator">
      <option value="+">+</option>
      <option value="-">-</option>
      <option value="*">*</option>
      <option value="/">/</option>
    </select>
    <input v-model="num2" type="number" placeholder="输入第二个数字">
    <button @click="calculate">计算</button>
    <p v-if="result!== null">结果: {{ result }}</p>
  </div>
  <script>
    const { createApp } = Vue;

    const app = createApp({
      data() {
        return {
          num1: '',
          num2: '',
          operator: '+',
          result: null
        };
      },
      methods: {
        calculate() {
          const n1 = parseFloat(this.num1);
          const n2 = parseFloat(this.num2);

          if (isNaN(n1) || isNaN(n2)) {
            alert('请输入有效的数字');
            return;
          }

          switch (this.operator) {
            case '+':
              this.result = n1 + n2;
              break;
            case '-':
              this.result = n1 - n2;
              break;
            case '*':
              this.result = n1 * n2;
              break;
            case '/':
              if (n2 === 0) {
                alert('除数不能为零');
                return;
              }
              this.result = n1 / n2;
              break;
          }
        }
      }
    });

    app.mount('#app');
  </script>
</body>

</html>